package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.bean.ItemPartner;
import com.wolfalpha.utron.view.CircleImageView;

import java.util.List;

/**
 * 合伙人列表适配器
 * <p/>
 * Created by luodon on 15/9/2.
 */
public class ItemPartnerAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private Context context = null;
    private List<ItemPartner> partners;


    public ItemPartnerAdapter(Context context, List<ItemPartner> rs) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        partners = rs;
    }

    public void setData(List<ItemPartner> rs) {
        if (rs != null) {
            partners = rs;
            this.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return partners.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_partner, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.item_tv_partner_name);
            viewHolder.tvDy = (TextView) convertView.findViewById(R.id.item_tv_partner_dy);
            viewHolder.tvAbili = (TextView) convertView.findViewById(R.id.item_tv_partner_ability);
            viewHolder.tvLevel = (TextView) convertView.findViewById(R.id.item_tv_partner_level);
            viewHolder.tvSchool = (TextView) convertView.findViewById(R.id.item_tv_partner_school);
            viewHolder.ivHead = (CircleImageView) convertView.findViewById(R.id.item_cv_partner_head);

            viewHolder.tvRole = (TextView) convertView.findViewById(R.id.item_tv_partner_role);
            viewHolder.ivRole = (ImageView) convertView.findViewById(R.id.item_iv_partner_role);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (partners != null) {
            //设置控件内容
            viewHolder.tvName.setText(partners.get(position).getUser_name());
            viewHolder.tvDy.setText(partners.get(position).getDy());
            viewHolder.tvAbili.setText("技能: " + (null == partners.get(position).getAbility() ? "无" : partners.get(position).getAbility()));
            int level = partners.get(position).getLevel();
            if (level == 1) {
                viewHolder.tvLevel.setText("优秀合伙人");
                viewHolder.tvLevel.setBackgroundResource(R.drawable.corner_text_yellow_normal);
            } else if (level == 2) {
                viewHolder.tvLevel.setText("优秀合伙人");
                viewHolder.tvLevel.setBackgroundResource(R.drawable.corner_text_yellow_normal);
            } else if (level == 8) {
                viewHolder.tvLevel.setText("认证合伙人");
                viewHolder.tvLevel.setBackgroundResource(R.drawable.corner_text_orange_normal);
            } else {
                viewHolder.tvLevel.setText("普通合伙人");
                viewHolder.tvLevel.setBackgroundResource(R.drawable.corner_text_green_normal);
            }
            viewHolder.tvSchool.setText("来自: " + partners.get(position).getSchool());
            Picasso.with(context)
                    .load(partners.get(position).getUser_head())
                    .placeholder(R.mipmap.head_default)
                    .into(viewHolder.ivHead);
        }
        String string = partners.get(position).getRole();
        if (string != null) {
            if (string.equals("技术")) {
                viewHolder.tvRole.setText("技术");
                viewHolder.ivRole.setImageResource(R.mipmap.item_project_calls_tech_selected);
            }
            if (string.equals("运营")) {
                viewHolder.tvRole.setText("运营");
                viewHolder.ivRole.setImageResource(R.mipmap.item_project_calls_operation_selected);
            }
            if (string.equals("产品")) {
                viewHolder.tvRole.setText("产品");
                viewHolder.ivRole.setImageResource(R.mipmap.item_project_calls_product_selected);
            }
            if (string.equals("设计")) {
                viewHolder.tvRole.setText("设计");
                viewHolder.ivRole.setImageResource(R.mipmap.item_project_calls_design_selected);
            }
            if (string.equals("营销")) {
                viewHolder.tvRole.setText("营销");
                viewHolder.ivRole.setImageResource(R.mipmap.item_project_calls_sale_selected);
            }
        }
        return convertView;
    }

    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvName;        //合伙人昵称
        private TextView tvSchool;      //学校
        private TextView tvDy;          //代言
        private TextView tvAbili;       //技能
        private TextView tvLevel;       //等级

        private CircleImageView ivHead; //头像

        private TextView tvRole;        //角色
        private ImageView ivRole;       //角色图标
    }
}
